package org.metasketch.editor.links;

import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.DefaultLink;
import org.metasketch.diagram.javafx.graphics.LineControlPoint;
import org.metasketch.graphics.Point;

import javafx.scene.shape.Line;

public class LineSection extends Line {
	Point start;
	Point finish;
	private LineControlPoint finishControlPoint;
	private LineControlPoint startControlPoint;
	private BaseGraphic source;
	private BaseGraphic target;

	protected LineEnd sourceLineEnd;
	protected LineEnd targetLineEnd;

	DefaultLink link;

	public LineSection(DefaultLink defaultLink) {
		this.link = defaultLink;
	}

	public DefaultLink getLink() {
		return link;
	}

	public void setLink(DefaultLink link) {
		this.link = link;
	}

	public Point getStart() {
		return start;
	}

	public void setStart(Point start) {
		this.start = start;
	}

	public Point getFinish() {
		return finish;
	}

	public void setFinish(Point finish) {
		this.finish = finish;
	}

	public void changeStart(int x, int y) {
		start.setX(x);
		start.setY(y);

		setStartX(x);
		setStartY(y);

		if (startControlPoint != null) {
			startControlPoint.setTranslateX(x);
			startControlPoint.setTranslateY(y);

			// if(source!=null)
			// {
			// source
			// }
		}

		if (sourceLineEnd != null)
			sourceLineEnd
					.setNewPosition(x, y, (int) getEndX(), (int) getEndY());
	}

	public void changeFinish(int x, int y) {
		finish.setX(x);
		finish.setY(y);

		setEndX(x);
		setEndY(y);

		if (finishControlPoint != null) {
			finishControlPoint.setTranslateX(x);
			finishControlPoint.setTranslateY(y);
		}

		if (targetLineEnd != null)
			targetLineEnd.setNewPosition(x, y, (int) getStartX(),
					(int) getStartY());

	}

	public void setFinishControlPoint(LineControlPoint lineControlPoint) {
		this.finishControlPoint = lineControlPoint;

	}

	public void setStartControlPoint(LineControlPoint lineControlPoint) {
		this.startControlPoint = lineControlPoint;

	}

	public void setSourceObject(BaseGraphic source) {
		this.source = source;

	}

	public void setTargetObject(BaseGraphic target) {
		this.target = target;

	}

	public BaseGraphic getSource() {
		return source;
	}

	public BaseGraphic getTarget() {
		return target;
	}

	public void refreshNodeLink() {
		if (source != null) {
			link.refreshSource();
		}

		if (target != null) {
			link.refreshTarget();
		}
		
		link.refreshLineDecorations();

	}

	public LineEnd getSourceLineEnd() {
		return sourceLineEnd;
	}

	public void setSourceLineEnd(LineEnd sourceLineEnd) {
		this.sourceLineEnd = sourceLineEnd;
	}

	public LineEnd getTargetLineEnd() {
		return targetLineEnd;
	}

	public void setTargetLineEnd(LineEnd targetLineEnd) {
		this.targetLineEnd = targetLineEnd;
	}

}
